import {request} from '../../network/index'
let time;
let history = []
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		isShow:true,
		value:'',
		placeholder:'',
		hotSearch:'',
		searchData:[],
		history:[]
	},
	search(event){
		let value = event.detail.value
		if(value.trim()){
			this.setData({
				isShow:false
			})
			clearTimeout(time)
			time = setTimeout(()=>{
				request('/search/suggest',{keywords:value,type:'mobile'}).then(res=>{
					this.setData({
						searchData:res.data.result.allMatch
					})
				})
				},300)
		}else if(value.length == 0 || !this.data.value.length == 0){
			this.setData({
				isShow:true,
				searchData:[]
			})
		}
	},
	toSearch(event){
		if(event.detail.value.trim()){
			history.unshift(event.detail.value)
			this.setData({
				isShow:true,
				history
			})
			wx.navigateTo({
				url:'/pages/searchDetail/searchDetail',
				success(res){
					res.eventChannel.emit('search',event.detail.value) 
				}
			})
		}
	},
	clear(){
		this.setData({
			value:'',
			isShow:true
		})
	},
	searchHot(event){
		history.unshift(event.currentTarget.dataset.hot)
		this.setData({
			isShow:true,
			history
		})
		wx.navigateTo({
			url:'/pages/searchDetail/searchDetail',
			success(res){
				res.eventChannel.emit('search',event.currentTarget.dataset.hot) 
			}
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function (options) {
		let placeholder = await request('/search/default')
		let hotSearch = await request('/search/hot/detail')
		
		this.setData({
			placeholder:placeholder.data.data.showKeyword,
			hotSearch:hotSearch.data.data
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})